package com.kebabeaters.nakebab.client.ui.view;

import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent.HasSelectionChangedHandlers;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.kebabeaters.nakebab.client.presenter.TasterKebabRankingPresenter;
import com.kebabeaters.nakebab.client.presenter.TasterKebabRankingPresenter.ListEntry;

public class TasterKebabRankingView extends ViewImpl implements
		TasterKebabRankingPresenter.MyView {

	private final Widget widget;
	private ListDataProvider<ListEntry> dataProvider;
	protected SingleSelectionModel<ListEntry> selectionModel;
	
	@UiField(provided = true)
	protected CellList<ListEntry> cellList;
	
	@UiField
	protected Label header;
	
	@UiField
	protected Label label;
	
	public static class KebabRankingCell extends AbstractCell<ListEntry> {		
		
		@Override
		public void render(Context context, ListEntry value, SafeHtmlBuilder sb) {
			if (value == null) {
				return;
			}
			
			sb.appendHtmlConstant("<table width='100%' style='border-bottom: 2px solid;'>");
			
			sb.appendHtmlConstant("<tr><td width='10%' align='CENTER' rowspan='" + (value.getSize() + 1) + "'><font size='7'>");
			sb.appendEscaped(Integer.toString(value.getLp()));
			sb.appendHtmlConstant("</font></td>");

		    sb.appendHtmlConstant("<td colspan='2' align='CENTER'><font size='5'>");
		    sb.appendEscaped(value.getRestaurant());
		    sb.appendHtmlConstant("</font></td></tr>");
		    	  
		    for (int i = 0; i < value.getSize(); i++) {
		    	sb.appendHtmlConstant("<tr><td width='10%'></td>");
			    
			    sb.appendHtmlConstant("<td width='65%' align='LEFT'>");
			    sb.appendEscaped(value.getTaster(i));
			    sb.appendHtmlConstant("</td>");
			    
			    sb.appendHtmlConstant("<td width='25%' align='LEFT'><b>");
			    sb.appendEscaped(Double.toString(value.getMark(i)));
			    sb.appendHtmlConstant("</b></td></tr>");
		    }			 
		    
		    sb.appendHtmlConstant("</table>");
		}
	}

	public interface Binder extends UiBinder<Widget, TasterKebabRankingView> {
	}

	@Inject
	public TasterKebabRankingView(final Binder binder) {
		KebabRankingCell cell = new KebabRankingCell();
		cellList = new CellList<ListEntry>(cell);
		
		selectionModel = new SingleSelectionModel<ListEntry>();
		cellList.setSelectionModel(selectionModel);
		
		dataProvider = new ListDataProvider<ListEntry>();
		dataProvider.addDataDisplay(cellList);
		
		widget = binder.createAndBindUi(this);
		
		header.setText("RANKINGI");
		label.setText("Ranking najlepszych kebabiarni według smakoszy");
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void createList(List<ListEntry> resultList) {

		if (resultList != null) {
			dataProvider.setList(resultList);
		} else
			cellList.setRowCount(0);

		cellList.redraw();		
	}

	@Override
	public HasSelectionChangedHandlers getSelectionChangedHandler() {
		return cellList.getSelectionModel();
	}

	@Override
	public ListEntry getSelectedObject() {
		return selectionModel.getSelectedObject();
	}
}
